package juc;


import java.util.concurrent.CyclicBarrier;
/*
CyclicBarrier演示
场景：收集七颗龙珠才可召唤神龙
 */
public class CyclicBarrierDemo {

  public static void main(String[] args) {

    int number = 7;
    CyclicBarrier cyclicBarrier = new CyclicBarrier(number,()->{
      System.out.println(Thread.currentThread().getName() + "  龙珠被收齐了，可以召唤神龙了");
    });

//    收集龙珠的过程
    for (int i = 1; i <= 7 ; i++) {

      new Thread(()->{
        System.out.println(Thread.currentThread().getName() + " 星龙珠被收集了");
//        等待
        try {
          cyclicBarrier.await();
        } catch (Exception e) {
          e.printStackTrace();
        }
      },String.valueOf(i)).start();

    }
  }
}
